
#include <cstdio>
#include <BMap>
#include <BState>
#include <BFile>
#include <BSpring>
#include <BSpacer>
#include "IReader.h"
#include "member_BResource.h"
#include "member_BWidget.h"
#include "member_BImage.h"
#include "member_BStyle.h"

using namespace BWE;

#define REGIST_CLASS(sign, name)\
	signMap[#name] = sign;

#define REGIST_OBJECT(sign, name)\
	readMap[sign] = &IReader::read##name;\
	textMap[sign] = #name;\
	REGIST_CLASS(sign, B##name)

#define REGIST_VALUE(sign, name)\
	readValueMap[sign] = &IReader::read##name;\
	textMap[sign] = #name;

#define bs_up	(stack.top().sign)
#define def #define
#define DEFINE(sign, number) def sign number

IReader::IReader()
{
	REGIST_OBJECT(SIGN_SCENE, Scene);
	REGIST_OBJECT(SIGN_NODE, Node);
	REGIST_OBJECT(SIGN_LIGHT, Light);
	REGIST_OBJECT(SIGN_CAMERA, Camera);
	REGIST_OBJECT(SIGN_PIXMAP, Pixmap);
	REGIST_OBJECT(SIGN_MESH, Mesh);
	REGIST_OBJECT(SIGN_GEOMETRY, Geometry);
	REGIST_OBJECT(SIGN_MATERIAL, Material);
	REGIST_OBJECT(SIGN_TEXTURE, Texture);
	REGIST_OBJECT(SIGN_PROGRAM, Program);
	REGIST_OBJECT(SIGN_IMAGE, Image);
	REGIST_OBJECT(SIGN_BITMAP, Bitmap);
	REGIST_OBJECT(SIGN_STATE, State);
	REGIST_OBJECT(SIGN_CONE, Cone);
	REGIST_OBJECT(SIGN_CUBE, Cube);
	REGIST_OBJECT(SIGN_CYLINDER, Cylinder);
	REGIST_OBJECT(SIGN_SPHERE, Sphere);
	REGIST_OBJECT(SIGN_TORUS, Torus);
	REGIST_OBJECT(SIGN_SURFACE, Surface);
	REGIST_OBJECT(SIGN_WIDGET, Widget);
	REGIST_OBJECT(SIGN_FONT, Font);
	REGIST_OBJECT(SIGN_CURSOR, Cursor);
	REGIST_OBJECT(SIGN_PALETTE, Palette);
	REGIST_OBJECT(SIGN_LAYOUT, Layout);
	REGIST_OBJECT(SIGN_SPRING, Spring);
	REGIST_OBJECT(SIGN_BUTTON, Button);
	REGIST_OBJECT(SIGN_SLIDER, Slider);
	REGIST_OBJECT(SIGN_COMBOBOX, ComboBox);
	REGIST_OBJECT(SIGN_CheckBox, CheckBox);
	REGIST_OBJECT(SIGN_SpinBox, SpinBox);
	REGIST_OBJECT(SIGN_LineEdit, LineEdit);
	REGIST_OBJECT(SIGN_TextEdit, TextEdit);
	REGIST_OBJECT(SIGN_TreeWidget, TreeWidget);
	REGIST_OBJECT(SIGN_ListWidget, ListWidget);
	REGIST_OBJECT(SIGN_TableWidget, TableWidget);
	REGIST_OBJECT(SIGN_ScrollBar, ScrollBar);
	REGIST_OBJECT(SIGN_ScrollWidget, ScrollWidget);
	REGIST_OBJECT(SIGN_Label, Label);
	REGIST_OBJECT(SIGN_Menu, Menu);
	REGIST_OBJECT(SIGN_MenuBar, MenuBar);
	REGIST_OBJECT(SIGN_Dialog, Dialog);
	REGIST_OBJECT(SIGN_GroupBox, GroupBox);
	REGIST_OBJECT(SIGN_Splitter, Splitter);
	REGIST_OBJECT(SIGN_TabWidget, TabWidget);
	REGIST_OBJECT(SIGN_Viewer, Viewer);

	REGIST_VALUE(SIGN_NAME, Name);
	REGIST_VALUE(SIGN_ENABLE, Enable);
	REGIST_VALUE(SIGN_MASK, Mask);
	REGIST_VALUE(SIGN_VISIBLE, Visible);
	REGIST_VALUE(SIGN_MATRIX, Matrix);
	REGIST_VALUE(SIGN_RECT, Rect);
	REGIST_VALUE(SIGN_SPACE, Space);
	REGIST_VALUE(SIGN_LIGHTING, Lighting);
	REGIST_VALUE(SIGN_DEPTHTEST, DepthTest);
	REGIST_VALUE(SIGN_LINESMOOTH, LineSmooth);
	REGIST_VALUE(SIGN_LINEWIDTH, LineWidth);
	REGIST_VALUE(SIGN_POINTSMOOTH, PointSmooth);
	REGIST_VALUE(SIGN_POINTSIZE, PointSize);
	REGIST_VALUE(SIGN_POLYGONMODE, PolygonMode);
	REGIST_VALUE(SIGN_CULLBACK, CullBack);
	REGIST_VALUE(SIGN_BASESIZE, BaseSize);
	REGIST_VALUE(SIGN_ROWCOUNT, RowCount);
	REGIST_VALUE(SIGN_COLUMNCOUNT, ColumnCount);
	REGIST_VALUE(SIGN_ROW, Row);
	REGIST_VALUE(SIGN_COLUMN, Column);
	REGIST_VALUE(SIGN_VERTICES, Vertices);
	REGIST_VALUE(SIGN_NORMALS, Normals);
	REGIST_VALUE(SIGN_TEXCOORDS, TexCoords);
	REGIST_VALUE(SIGN_FACES, Faces);
	REGIST_VALUE(SIGN_AMBIENT, Ambient);
	REGIST_VALUE(SIGN_DIFFUSE, Diffuse);
	REGIST_VALUE(SIGN_SPECULAR, Specular);
	REGIST_VALUE(SIGN_EMISSION, Emission);
	REGIST_VALUE(SIGN_SHININESS, Shininess);
	REGIST_VALUE(SIGN_ATTENUATION, Attenuation);
	REGIST_VALUE(SIGN_ATTENUATION2, Attenuation2);
	REGIST_VALUE(SIGN_POSITION, Position);
	REGIST_VALUE(SIGN_CENTER, Center);
	REGIST_VALUE(SIGN_UP, Up);
	REGIST_VALUE(SIGN_BLEND, Blend);
	REGIST_VALUE(SIGN_BLENDCOLOR, BlendColor);
	REGIST_VALUE(SIGN_FILTER, Filter);
	REGIST_VALUE(SIGN_WRAP, Wrap);
	REGIST_VALUE(SIGN_MIPMAP, Mipmap);
	REGIST_VALUE(SIGN_FILENAME, FileName);
	REGIST_VALUE(SIGN_FORMAT, Format);
	REGIST_VALUE(SIGN_SIZE, Size);
	REGIST_VALUE(SIGN_PIXELS, Pixels);
	REGIST_VALUE(SIGN_TITLE, Title);
	REGIST_VALUE(SIGN_TOOLTIP, Tip);
	REGIST_VALUE(SIGN_MODAL, Modal);
	REGIST_VALUE(SIGN_POSITION, Position);
	REGIST_VALUE(SIGN_MINSIZE, MinSize);
	REGIST_VALUE(SIGN_MAXSIZE, MaxSize);
	REGIST_VALUE(SIGN_STRETCH, Stretch);
	REGIST_VALUE(SIGN_SIZEPOLICY, SizePolicy);
	REGIST_VALUE(SIGN_BORDER, Border);
	REGIST_VALUE(SIGN_PERCH, Perch);
	REGIST_VALUE(SIGN_MARGIN, Margin);
	REGIST_VALUE(SIGN_COLOR, Color);
}
IReader::~IReader()
{

}

BObject* IReader::readObject(const BString& fileName)
{
	BString ext = fileName.ext();
	if (ext.empty())
		return 0;

	stack.clear();
	BFile file(fileName);
	if (!file.open(BFile::Mode_Read))
		return 0;
	stream = &file;

	if (!stream->read(block))
		return 0;

	Read read = readMap(block.sign);
	if (!read)
		return 0;

	BObject* object = (this->*read)(0);
	if (!object)
		return 0;
	ext.lower();
	if (ext == "widget" && !dynamic_cast<BWidget*>(object))
		return 0;
	else if (ext == "node" && !dynamic_cast<BNode*>(object))
		return 0;
	else if (ext == "scene" && !dynamic_cast<BScene*>(object))
		return 0;
	else if (ext == "image" && !dynamic_cast<BImage*>(object))
		return 0;
	else if (ext == "shape" && !dynamic_cast<BShape*>(object))
		return 0;
	else if (ext == "shape" && !dynamic_cast<BShape*>(object))
		return 0;
	else if (ext == "material" && !dynamic_cast<BMaterial*>(object))
		return 0;

	if (!readContent(object))
	{
		delete object;
		return 0;
	}
	return object;
}

bool IReader::readContent(BObject* object)
{
	stack.append(block);
	int end = block.end;
	while (stream->offset() < end)
	{
		stream->read(block);
		if (block.sign < SIGN_OBJECT_MAX)
		{
			Read read = readMap(block.sign);
			if (!read)
			{
				printf("Read : %s -- No read method.\n", textMap[block.sign]);
				return false;
			}
			BObject* subObject = (this->*read)(object);
			if (!subObject)
			{
				printf("Read : %s -- Read sub object faild.\n", textMap[block.sign]);
				return false;
			}
			if (!this->readContent(subObject))
			{
				printf("Read : %s -- Read object content faild.\n", textMap[block.sign]);
				return false;
			}
		}
		else
		{
			ReadValue readValue = readValueMap(block.sign);
			if (!readValue)
			{
				printf("Read : %s -- No value read method.\n", textMap[block.sign]);
				return false;
			}
			bool ret = (this->*readValue)(object);
			if (!ret)
			{
				printf("Read : %s -- Read value faild.\n", textMap[block.sign]);
				return false;
			}
		}
		if (stream->offset() != block.end)
		{
			printf("Read : %s -- Block range trouble.\n", textMap[block.sign]);
			return false;
		}
		stream->seek(block.end);
	}
	block = stack.last();
	stack.remove(stack.size() - 1);
	return true;
}

bool IReader::readName(BObject* object)
{
	BString name;
	stream->read(name);
	if (object)
	{
		object->setName(name);
		return true;
	}
	return false;
}
bool IReader::readEnable(BObject* object)
{
	bool able = true;
	stream->read(able);
	if (object)
	{
		object->setAble(able);
		return true;
	}
	return false;
}
bool IReader::readMask(BObject* object)
{
	unsigned int mask;
	stream->read(mask);
	if (object)
	{
		return true;
	}
	return false;
}
bool IReader::readAmbient(BObject* object)
{
	BColor ambient;
	stream->read(ambient);
	if (bs_up == SIGN_MATERIAL)
	{
		BMaterial* material = (BMaterial*)object;
		material->setAmbient(ambient);
		return true;
	}
	if (BLight* light = dynamic_cast<BLight*>(object))
	{
		light->setAmbient(ambient);
		return true;
	}
	return false;
}
bool IReader::readDiffuse(BObject* object)
{
	BColor diffuse;
	if (!stream->read(diffuse))
		return false;
	if (BLight* light = dynamic_cast<BLight*>(object))
	{
		light->setDiffuse(diffuse);
		return true;
	}
	if (BMaterial* material = dynamic_cast<BMaterial*>(object))
	{
		material->setDiffuse(diffuse);
		return true;
	}
	return false;
}
bool IReader::readSpecular(BObject* object)
{
	BColor specular;
	if (!stream->read(specular))
		return false;
	BLight* light = dynamic_cast<BLight*>(object);
	if (light)
	{
		light->setSpecular(specular);
		return true;
	}
	BMaterial* material = dynamic_cast<BMaterial*>(object);
	if (material)
	{
		material->setSpecular(specular);
		return true;
	}
	return true;
}
bool IReader::readEmission(BObject* object)
{
	BColor emission;
	if (!stream->read(emission))
		return false;
	BMaterial* material = dynamic_cast<BMaterial*>(object);
	if (material)
	{
		material->setEmission(emission);
		return true;
	}
	return false;
}
bool IReader::readShininess(BObject* object)
{
	float shininess;
	if (!stream->read(shininess))
		return false;
	BMaterial* material = dynamic_cast<BMaterial*>(object);
	if (material)
	{
		material->setShininess(shininess);
		return true;
	}
	return false;
}
bool IReader::readAttenuation(BObject* object)
{
	float attenuation;
	if (!stream->read(attenuation))
		return false;
	BLight* light = dynamic_cast<BLight*>(object);
	if (light)
	{
		light->setAttenuation(attenuation);
		return true;
	}
	return false;
}
bool IReader::readAttenuation2(BObject* object)
{
	float attenuation2;
	stream->read(attenuation2);

	BLight* light = dynamic_cast<BLight*>(object);
	if (light)
	{
		light->setAttenuation2(attenuation2);
		return true;
	}
	return false;
}
bool IReader::readPosition(BObject* object)
{
	BVector position;
	stream->read(position);

	BCamera* camera = dynamic_cast<BCamera*>(object);
	if (camera)
	{
		camera->setPosition(position);
		return true;
	}
	return false;
}
bool IReader::readCenter(BObject* object)
{
	BVector center;
	stream->read(center);
	BCamera* camera = dynamic_cast<BCamera*>(object);
	if (camera)
	{
		camera->setCenter(center);
		return true;
	}
	return false;
}
bool IReader::readUp(BObject* object)
{
	BVector vup;
	stream->read(vup);

	BCamera* camera = dynamic_cast<BCamera*>(object);
	if (camera)
	{
		camera->setUp(vup);
		return true;
	}
	return false;
}
bool IReader::readMatrix(BObject* object)
{
	BMatrix matrix;
	stream->read(matrix);
	if (BNode* node = dynamic_cast<BNode*>(object))
	{
		node->setMatrix(matrix);
		return true;
	}
	return false;
}
bool IReader::readVisible(BObject* object)
{
	bool visible;
	stream->read(visible);
	BNode* node = dynamic_cast<BNode*>(object);
	if (node)
	{
		node->setVisible(visible);
		return true;
	}
	return false;
}
bool IReader::readVertices(BObject* object)
{
	BVectorArray* vertices = new BVectorArray();
	if (!stream->read(*vertices))
		return false;
	if (BMesh* mesh = dynamic_cast<BMesh*>(object))
	{
		mesh->setVertices(vertices);
		return true;
	}
	if (BGeometry* geometry = dynamic_cast<BGeometry*>(object))
	{
		geometry->setVertices(vertices);
		return true;
	}
	return false;
}
bool IReader::readNormals(BObject* object)
{
	BVectorArray* normals = new  BVectorArray();
	if (!stream->read(*normals))
		return false;
	if (BMesh* mesh = dynamic_cast<BMesh*>(object))
	{
		mesh->setNormals(normals);
		return true;
	}
	if (BGeometry* geometry = dynamic_cast<BGeometry*>(object))
	{
		geometry->setNormals(normals);
		return true;
	}
	return false;
}
bool IReader::readFaces(BObject* object)
{
	BFaceArray* faces = new BFaceArray();
	if (!stream->read(*faces))
		return false;
	if (BMesh* mesh = dynamic_cast<BMesh*>(object))
	{
		mesh->setFaces(faces);
		return true;
	}
	return false;
}
bool IReader::readTexCoords(BObject* object)
{
	BCoordArray* texCoords = new BCoordArray();
	if (!stream->read(*texCoords))
		return false;
	BMesh* mesh = dynamic_cast<BMesh*>(object);
	if (mesh)
	{
		mesh->setTexCoords(texCoords);
		return true;
	}
	BGeometry* geometry = dynamic_cast<BGeometry*>(object);
	if (geometry)
	{
		geometry->setTexCoords(texCoords);
		return true;
	}
	return false;
}
bool IReader::readFileName(BObject* object)
{
	BString fileName;
	stream->read(fileName);
	if (bs_up == SIGN_IMAGE)
	{
		BImage* image = (BImage*)object;
		//image->setFileName(fileName);
		return true;
	}
	return true;
}
bool IReader::readTitle(BObject* object)
{
	BString title;
	stream->read(title);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setTitle(title);
		return true;
	}
	return false;
}
bool IReader::readTip(BObject* object)
{
	BString tips;
	stream->read(tips);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setTips(tips);
		return true;
	}
	return false;
}
bool IReader::readModal(BObject* object)
{
	bool modal;
	stream->read(modal);
	BWidget* widget = (BWidget*)object;
	if (widget)
	{
		widget->setModal(modal);
		return true;
	}
	return false;
}
bool IReader::readPos(BObject* object)
{
	BPoint pos;
	stream->read(pos);
	BWidget* widget = (BWidget*)object;
	if (widget)
	{
		widget->setPos(pos);
		return true;
	}
	return false;
}
bool IReader::readSize(BObject* object)
{
	BSize size;
	stream->read(size);
	if (bs_up == SIGN_WIDGET)
	{
		BWidget* widget = dynamic_cast<BWidget*>(object);
		widget->setSize(size);
		return true;
	}
	if (bs_up == SIGN_IMAGE)
	{
		BImage* image = dynamic_cast<BImage*>(object);
		image->setSize(size);
		return true;
	}
	if (bs_up == SIGN_PIXMAP)
	{
		BDisplay* pixmap = (BDisplay*)object;
		pixmap->setSize(size);
		return true;
	}
	return false;
}
bool IReader::readMinSize(BObject* object)
{
	BSize minSize;
	stream->read(minSize);
	if (BWidget* widget = (BWidget*)object)
	{
		widget->setMinSize(minSize);
		return true;
	}
	return false;
}
bool IReader::readMaxSize(BObject* object)
{
	BSize maxSize;
	stream->read(maxSize);
	if (BWidget* widget = (BWidget*)object)
	{
		widget->setMaxSize(maxSize);
		return true;
	}
	return false;
}
bool IReader::readStretch(BObject* object)
{
	float sizeStretch;
	stream->read(sizeStretch);
	if (BWidget* widget = (BWidget*)object)
	{
		widget->setSizeStretch(sizeStretch);
		return true;
	}
	return false;
}
bool IReader::readSizePolicy(BObject* object)
{
	BSizePolicy sizePolicy;
	stream->read(sizePolicy);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setSizePolicy(sizePolicy);
		return true;
	}
	return false;
}
bool IReader::readBorder(BObject* object)
{
	int border;
	stream->read(border);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setBorder(border);
		return true;
	}
	return false;
}
bool IReader::readPerch(BObject* object)
{
	BPerchs perchs;
	stream->read(perchs);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setPerchs(perchs);
		return true;
	}
	return false;
}
bool IReader::readMargin(BObject* object)
{
	BMargins margins;
	stream->read(margins);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setMargins(margins);
		return true;
	}
	return false;
}
bool IReader::readRect(BObject* object)
{
	BRect rect;
	stream->read(rect);
	if (BGridLayout* layout = dynamic_cast<BGridLayout*>(object))
	{
		layout->setRect(rect);
		return true;
	}
	return false;
}
bool IReader::readSpace(BObject* object)
{
	int space;
	stream->read(space);
	if (BGridLayout* layout = dynamic_cast<BGridLayout*>(object))
	{
		layout->setSpace(space);
		return true;
	}
	return false;
}
bool IReader::readRowCount(BObject* object)
{
	int rowCount;
	stream->read(rowCount);
	if (BGridLayout* layout = dynamic_cast<BGridLayout*>(object))
	{

		return true;
	}
	return false;
}
bool IReader::readColumnCount(BObject* object)
{
	int columnCount;
	stream->read(columnCount);
	if (BGridLayout* layout = dynamic_cast<BGridLayout*>(object))
	{

		return true;
	}
	return false;
}
bool IReader::readRow(BObject* object)
{
	return true;
}
bool IReader::readColumn(BObject* object)
{
	return true;
}
bool IReader::readBlend(BObject* object)
{
	int blend = 0;
	stream->read(blend);
	if (BTexture* texture = dynamic_cast<BTexture*>(object))
	{
		texture->setBlend((BTexture::Blend)blend);
		return true;
	}
	return false;
}
bool IReader::readBlendColor(BObject* object)
{
	BColor color;
	if (!stream->read(color))
		return false;
	if (BTexture* texture = dynamic_cast<BTexture*>(object))
	{
		texture->setBlendColor(color);
		return true;
	}
	return false;
}
bool IReader::readFilter(BObject* object)
{
	int filter = 0;
	if (!stream->read(filter))
		return false;
	if (BTexture* texture = dynamic_cast<BTexture*>(object))
	{
		texture->setFilter((BTexture::Filter)filter);
		return true;
	}
	return false;
}
bool IReader::readWrap(BObject* object)
{
	int wrap = 0;
	if (!stream->read(wrap))
		return false;
	if (BTexture* texture = dynamic_cast<BTexture*>(object))
	{
		texture->setWrap((BTexture::Wrap)wrap);
		return true;
	}
	return false;
}
bool IReader::readMipmap(BObject* object)
{
	bool mipmap;
	if (!stream->read(mipmap))
		return false;
	if (BTexture* texture = dynamic_cast<BTexture*>(object))
	{
		texture->setMipmap(mipmap);
		return true;
	}
	return false;
}
bool IReader::readFormat(BObject* object)
{
	Format format;
	stream->read(format);
	if (BImage* image = dynamic_cast<BImage*>(object))
	{
		image->setFormat(format);
		return true;
	}
	return false;
}
bool IReader::readPixels(BObject* object)
{
	BByteArray* pixels = new BByteArray();
	if (!stream->read(*pixels))
		return false;
	if (BImage* image = dynamic_cast<BImage*>(object))
	{
		image->setPixels(pixels);
		return true;
	}
	return false;
}
bool IReader::readLighting(BObject* object)
{
	bool lighting;
	stream->read(lighting);
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setLighting(lighting);
		return true;
	}
	return false;
}
bool IReader::readDepthTest(BObject* object)
{
	bool depthTest;
	if (!stream->read(depthTest))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setDepthTest(depthTest);
		return true;
	}
	return false;
}
bool IReader::readLineSmooth(BObject* object)
{
	bool lineSmooth;
	if (!stream->read(lineSmooth))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setLineSmooth(lineSmooth);
		return true;
	}
	return false;
}
bool IReader::readLineWidth(BObject* object)
{
	int lineWidth;
	if (!stream->read(lineWidth))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setLineWidth(lineWidth);
		return true;
	}
	return false;
}
bool IReader::readPointSmooth(BObject* object)
{
	bool pointSmooth;
	if (!stream->read(pointSmooth))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setPointSmooth(pointSmooth);
		return true;
	}
	return false;
}
bool IReader::readPointSize(BObject* object)
{
	int pointSize;
	if (!stream->read(pointSize))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setPointSize(pointSize);
		return true;
	}
	return false;
}
bool IReader::readPolygonMode(BObject* object)
{
	PolygonMode polygonMode;
	if (!stream->read(polygonMode))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setPolygonMode(polygonMode);
		return true;
	}
	return false;
}
bool IReader::readCullBack(BObject* object)
{
	bool cullBackFace;
	if (!stream->read(cullBackFace))
		return false;
	if (bs_up == SIGN_STATE)
	{
		BState* state = (BState*)object;
		state->setCullBack(cullBackFace);
		return true;
	}
	return false;
}
bool IReader::readBaseSize(BObject* object)
{
	BSize baseSize;
	stream->read(baseSize);
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setSize(baseSize);
		return true;
	}
	return false;
}
bool IReader::readPixelSize(BObject* object)
{
	BSize pixelSize;
	if (!stream->read(pixelSize))
		return false;
	if (bs_up == SIGN_PIXMAP)
	{
		BDisplay* pixmap = (BDisplay*)object;
		pixmap->setSize(pixelSize);
		return true;
	}
	return false;
}
bool IReader::readColor(BObject* object)
{
	BColor color;
	if (!stream->read(color))
		return false;
	if (bs_up == SIGN_PIXMAP)
	{
		BDisplay* pixmap = (BDisplay*)object;
		pixmap->setBackground(color);
		return true;
	}
	return false;
}

BObject* IReader::readNode(BObject* object)
{
	BNode* node = new BNode();
	if (BScene* scene = dynamic_cast<BScene*>(object))
	{
		scene->addNode(node);
	}
	return node;
}
BObject* IReader::readScene(BObject* object)
{
	BScene* scene = new BScene();

	return scene;
}
BObject* IReader::readLight(BObject* object)
{
	BLight* light = new BLight();
	if (BScene* scene = dynamic_cast<BScene*>(object))
	{
		scene->addNode(light);
	}
	return light;
}
BObject* IReader::readCamera(BObject* object)
{
	BCamera* camera = new BCamera();
	if (bs_up == SIGN_Viewer)
	{
		BViewer* viewer = dynamic_cast<BViewer*>(object);
		viewer->setCamera(camera);
	}
	return camera;
}
BObject* IReader::readMaterial(BObject* object)
{
	BMaterial* material = new BMaterial();
	if (BMesh* mesh = dynamic_cast<BMesh*>(object))
	{
		mesh->setMaterial(material);
	}
	if (BGeometry* geometry = dynamic_cast<BGeometry*>(object))
	{
		geometry->setMaterial(material);
	}
	return material;
}
BObject* IReader::readTexture(BObject* object)
{
	BTexture* texture = 0;
	if (BShape* shape = dynamic_cast<BShape*>(object))
	{
		shape->setTexture(texture);
	}
	return texture;
}
BObject* IReader::readProgram(BObject* object)
{
	BProgram* program = new BProgram();
	if (BShape* shape = dynamic_cast<BShape*>(object))
	{
		shape->setProgram(program);
	}
	return program;
}
BObject* IReader::readState(BObject* object)
{
	BState* state = new BState();
	if (BNode* node = dynamic_cast<BNode*>(object))
	{
		node->setState(state);
	}
	return state;
}
BObject* IReader::readImage(BObject* object)
{
	BImage* image = new BImage();
	//if (BMesh* mesh = dynamic_cast<BMesh*>(object))
	//{
	//	mesh->setTexture(image);
	//}
	//if (BGeometry* geometry = dynamic_cast<BGeometry*>(object))
	//{
	//	geometry->setTexture(image);
	//}
	//if (bs_up == SIGN_RESOURCE)
	//{
	//	BResource* resource = dynamic_cast<BResource*>(object);
	//	resource->addImage(image);
	//}
	return image;
}
BObject* IReader::readPixmap(BObject* object)
{
	BDisplay* pixmap = new BDisplay();
	return pixmap;
}
BObject* IReader::readMesh(BObject* object)
{
	BMesh* mesh = new BMesh();
	if (BNode* node = dynamic_cast<BNode*>(object))
	{
		node->addShape(mesh);
	}
	return mesh;
}
BObject* IReader::readGeometry(BObject* object)
{
	BGeometry* geometry = new BGeometry();
	if (BNode* node = dynamic_cast<BNode*>(object))
	{
		node->addShape(geometry);
	}
	return geometry;
}
BObject* IReader::readCone(BObject* object)
{
	BCone* cone = new BCone();
	return cone;
}
BObject* IReader::readCube(BObject* object)
{
	BCube* cube = new BCube();
	return cube;
}
BObject* IReader::readCylinder(BObject* object)
{
	BCylinder* cylinder = new BCylinder();
	return cylinder;
}
BObject* IReader::readSphere(BObject* object)
{
	BSphere* sphere = new BSphere();
	return sphere;
}
BObject* IReader::readSurface(BObject* object)
{
	BSurface* surface = new BSurface();
	return surface;
}
BObject* IReader::readTorus(BObject* object)
{
	BTorus* torus = new BTorus();
	return torus;
}
BObject* IReader::readWidget(BObject* object)
{
	BWidget* widget = new BWidget();
	if (BWidget* parent = dynamic_cast<BWidget*>(object))
	{
		parent->addChild(widget);
	}
	if (bs_up == SIGN_LAYOUT)
	{
		//((BGridLayout*)object)->addWidget(widget);
	}
	return widget;
}
BObject* IReader::readLayout(BObject* object)
{
	BGridLayout* layout = new BGridLayout();
	if (bs_up == SIGN_WIDGET)
	{
		if(BWidget* host = dynamic_cast<BWidget*>(object))
			host->setLayout(layout);
	}
	if (bs_up == SIGN_LAYOUT)
	{

	}
	return layout;
}
BObject* IReader::readSpring(BObject* object)
{
	BSpring* spring = new BSpring();
	if (BGridLayout* layout = dynamic_cast<BGridLayout*>(object))
	{
		//layout->addSpring(spring);
		return spring;
	}
	return spring;
}
BObject* IReader::readCursor(BObject* object)
{
	BCursor* cursor = new BCursor();
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setCursor(cursor);
	}
	return cursor;
}
BObject* IReader::readPalette(BObject* object)
{
	BStyle* style = new BStyle();
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setStyle(style);
	}
	return style;
}
BObject* IReader::readFont(BObject* object)
{
	BFont* font = new BFont();
	if (BWidget* widget = dynamic_cast<BWidget*>(object))
	{
		widget->setFont(font);
	}
	return font;
}
BObject* IReader::readButton(BObject* object)
{
	BButton* button = new BButton();
	return button;
}
BObject* IReader::readSlider(BObject* object)
{
	BSlider* slider = new BSlider();
	return slider;
}
BObject* IReader::readComboBox(BObject* object)
{
	BComboBox* comboBox = new BComboBox();
	return comboBox;
}
BObject* IReader::readCheckBox(BObject* object)
{
	BCheckBox* checkBox = new BCheckBox();
	return checkBox;
}
BObject* IReader::readSpinBox(BObject* object)
{
	BSpinBox* spinBox = new BSpinBox();
	return spinBox;
}
BObject* IReader::readLineEdit(BObject* object)
{
	BLineEdit* lineEdit = new BLineEdit();
	return lineEdit;
}
BObject* IReader::readTextEdit(BObject* object)
{
	BTextEdit* textEdit = new BTextEdit();
	return textEdit;
}
BObject* IReader::readTreeWidget(BObject* object)
{
	BTreeWidget* treeWidget = new BTreeWidget();
	return treeWidget;
}
BObject* IReader::readListWidget(BObject* object)
{
	BListWidget* listWidget = new BListWidget();
	return listWidget;
}
BObject* IReader::readTableWidget(BObject* object)
{
	BTableWidget* tableWidget = new BTableWidget();
	return tableWidget;
}
BObject* IReader::readScrollBar(BObject* object)
{
	BScrollBar* scrollBar = new BScrollBar();
	return scrollBar;
}
BObject* IReader::readScrollWidget(BObject* object)
{
	BScrollWidget* scrollWidget = new BScrollWidget();
	return scrollWidget;
}
BObject* IReader::readLabel(BObject* object)
{
	BLabel* label = new BLabel();
	return label;
}
BObject* IReader::readMenu(BObject* object)
{
	BMenu* menu = new BMenu();
	return menu;
}
BObject* IReader::readMenuBar(BObject* object)
{
	BMenuBar* menuBar = new BMenuBar();
	return menuBar;
}
BObject* IReader::readDialog(BObject* object)
{
	BDialog* dialog = new BDialog();
	return dialog;
}
BObject* IReader::readGroupBox(BObject* object)
{
	BGroupBox* groupBox = new BGroupBox();
	return groupBox;
}
BObject* IReader::readSplitter(BObject* object)
{
	BSplitter* splitter = new BSplitter();
	return splitter;
}
BObject* IReader::readTabWidget(BObject* object)
{
	BTabWidget* tabWidget = new BTabWidget();
	return tabWidget;
}
BObject* IReader::readViewer(BObject* object)
{
	BViewer* viewer = new BViewer();
	return viewer;
}
BObject* IReader::readBitmap(BObject* object)
{
	BImage* bitmap = new BImage();
	return bitmap;
}

